﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QL_DTTN.DAL;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
namespace QL_DTTN.DTO.DTO
{
  public partial class DtoClass
    {
      protected  string _tableName="";
      protected  string _keyField = "";
      protected  ArrayList _keyFields;
      public DtoClass()
      {
          _keyField = "Ma" + _tableName;
          _keyFields = new ArrayList();
      }
      public bool Xoa(object ma)
      {
        SqlDataProvider dp = new SqlDataProvider();
        dp.AddParameter(new SqlParameter("@pMa",ma));
        return dp.RunSQL(String.Format(" Delete from {0} where {1} = @pMa ", _tableName, _keyField), true);
      }
      public bool Xoa(object ma1,object ma2)
      {
          SqlDataProvider dp = new SqlDataProvider();
          dp.AddParameter(new SqlParameter("@pMa1", ma1));
          dp.AddParameter(new SqlParameter("@pMa2", ma2));
          return dp.RunSQL(String.Format(" Delete from {0} where {1} = @pMa1 and {2} = @pMa2 ", _tableName, _keyFields[0],_keyFields[1]), true);
      }
      public bool Xoas(List<object> lstMa)
      {
         SqlDataProvider dp = new SqlDataProvider();
         int i = 0;
         string sql = String.Format(" delete from {0} where {1} on (", _tableName, _keyField);
         foreach (var item in lstMa)
         {   
             dp.AddParameter(new SqlParameter("@pMa"+i,item));
             sql += "@pMa"+i;
         }
         sql = sql.Substring(0, sql.Length - 1) + ")";
         return  dp.RunSQL(sql,true);
      }
      protected  SqlDataReader selectAll(SqlDataProvider dp)
      {
          return dp.ExDataReader("select * from "+_tableName,true);
      }
      public  DataTable selectAll()
      {
          SqlDataProvider dp = new SqlDataProvider();
          return dp.ExDataTable(" select * from " + _tableName + " order by "+_keyField+" DESC ", true);
      }
      protected  SqlDataReader GetObject(object ma, SqlDataProvider dp)
      {
          dp.AddParameter(new SqlParameter("@pMa", ma));
          return dp.ExDataReader(String.Format(" select * from {0} where {1} = @pMa  order by {1} DESC  ", _tableName, _keyField), true);
      }
      protected  SqlDataReader GetObject(object ma1, object ma2, SqlDataProvider dp)
      {
          dp.AddParameter(new SqlParameter("@pMa1", ma1));
          dp.AddParameter(new SqlParameter("@pMa2", ma2));
          return dp.ExDataReader(String.Format(" select * from {0} where {1} = @pMa1 and {2} = @pMa2 ", _tableName, _keyFields[0], _keyFields[1]), true);
      }
    }
}
